import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  UpdateDateColumn,
  CreateDateColumn,
  OneToMany,
} from 'typeorm';
import { User } from './user.entity';
import { VideoHistory } from './video-history.entity';
import { VideoCategory } from './video-category.entity';

@Entity()
export class Video {
  @PrimaryGeneratedColumn()
  video_id: number;

  @ManyToOne(() => User, (user) => user.videos)
  @JoinColumn({ name: 'user_id' })
  user: User;

  @Column({ length: 255 })
  title: string;

  @Column({ type: 'text' })
  description: string;

  @Column({ length: 512 })
  oss_url: string;

  @Column({ length: 512, nullable: true })
  cover_url: string;

  // 新增视频时长字段，以秒为单位
  @Column({
    type: 'int',
    nullable: true,
    comment: '视频时长（以秒为单位）',
  })
  duration: number;

  @Column({
    type: 'enum',
    enum: ['pending', 'approved', 'rejected', 'hidden', 'manualReview'],
    default: 'pending',
    comment: 'pending-待审核, approved-已通过, rejected-已拒绝, hidden-已隐藏, manualReview-人工审核',
  })
  status: string;

  @Column({ type: 'text', nullable: true })
  audit_reason: string;

  @Column({ nullable: true })
  censor_job_id: string;

  @CreateDateColumn({
    type: 'datetime',
    comment: '创建时间',
  })
  created_at: Date;

  @UpdateDateColumn({
    type: 'datetime',
    comment: '更新时间',
  })
  updated_at: Date;

  @Column({
    type: 'tinyint',
    width: 1,
    default: 0,
    comment: '是否删除 0-否 1-是',
  })
  is_delete: number;

  @Column({ default: 0 })
  likes_count: number;

  @OneToMany(() => VideoHistory, (videoHistory) => videoHistory.video)
  viewHistories: VideoHistory[];

  // 分类关联
  @ManyToOne(() => VideoCategory, (category) => category.videos, {
    nullable: true,
  })
  @JoinColumn({ name: 'category_id' })
  category: VideoCategory;
}
